package de.openlaboratory.elib.controller.library;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

import de.openlaboratory.elib.controller.ElibService;
import de.openlaboratory.elib.controller.PublicationDAO;
import de.openlaboratory.elib.core.LogManager;
import de.openlaboratory.elib.model.dao.PublicationCollection;
import de.openlaboratory.elib.model.library.Library;
import de.openlaboratory.elib.model.library.Publication;
import de.openlaboratory.elib.utilities.Operation;

public class PublicationFacade implements ElibService
{
	@Override
	public void destroy()
	{
		// TODO Auto-generated method stub

	}

	@Override
	public ServletConfig getServletConfig()
	{
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String getServletInfo()
	{
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void init(ServletConfig arg0) throws ServletException
	{
		// TODO Auto-generated method stub

	}

	@Override
	public void service(ServletRequest arg0, ServletResponse arg1) throws ServletException, IOException
	{
		// TODO Auto-generated method stub

	}

	@Override
	public List<Publication> execute(Operation operation, Object obj)
	{		
		List<Publication> publications = new ArrayList<Publication>();
		PublicationDAO dao = new PublicationDAO();
		Publication p;
		
		switch (operation)
		{
			case READ:
				p = new PublicationDAO().find((Long) obj);
				publications.add(p);
				LogManager.logMessage("Publication loaded [" + obj.toString() + "]");
				break;
			case CREATE:
				Long id = dao.create((Publication) obj);
				publications.add(dao.find(id));
				LogManager.logMessage("Library created [" + id + "]");
				break;
			case UPDATE:
				dao.update((Publication) obj);
				p = dao.find(((Publication) obj).getId());
				publications.add(p);
				LogManager.logMessage("Library updated [" + p.getId().toString() + "]");
				break;
			case DELETE:
				p = dao.find((Long) obj);
				dao.delete(p);
				LogManager.logMessage("Library deleted [" + p.getId().toString() + "]");
				break;
			default:
				publications = listAllPublications();
				break;
		}

		return publications;
	}

	public List<Publication> listAllPublications()
	{
		List<Publication> publications = new ArrayList<Publication>();
		publications = new PublicationDAO().findAll();
		
		return publications;
	}

	public PublicationCollection list(Library library)
	{
		return null;
	}

	@Override
	public String getName() {
		// TODO Auto-generated method stub
		return "Publication Service Facade";
	}

	@Override
	public List<?> execute(String command, Object obj) {
		// TODO Auto-generated method stub
		return null;
	}
}
